package com.thinkgem.jeesite.modules.sys.service;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.modules.sys.dao.CheckAllDao;
import com.thinkgem.jeesite.modules.sys.dao.ShopAddDao;
import com.thinkgem.jeesite.modules.sys.dao.TenementPayInfoDao;
import com.thinkgem.jeesite.modules.sys.entity.TbOfflineShoppingOrderLog;
import com.thinkgem.jeesite.modules.sys.entity.TbUserTenementInfo;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by loutong on 2016/11/2.
 */
@Service
public class TenementPayInfoService {
    @Autowired
    private TenementPayInfoDao tenementPayInfoDao;
    @Autowired
    private ShopAddDao shopAddDao;
    @Autowired
    private CheckAllDao checkAllDao;

    //代理商查看所代理的市下的物业缴费记录
    @Transactional(readOnly = true)
    public  Page<TbUserTenementInfo> findByOrderUid(Page<TbUserTenementInfo> tbUserTenementInfoPage, TbUserTenementInfo tbUserTenementInfo){
        tbUserTenementInfo.setPage(tbUserTenementInfoPage);
        tbUserTenementInfo.setProvinceName(UserUtils.getUser().getProvinces());
        tbUserTenementInfo.setCityName(UserUtils.getUser().getCitys());

      /*  if (tbUserTenementInfo.getBeginDate() == null){
            tbUserTenementInfo.setBeginDate(DateUtils.setDays(DateUtils.parseDate(DateUtils.getDate()), 1));
        }
        if (tbUserTenementInfo.getEndDate() == null){
            tbUserTenementInfo.setEndDate(DateUtils.addMonths(tbUserTenementInfo.getBeginDate(), 1));
        }*/
        Date date=tbUserTenementInfo.getEndDate();
        if(date!=null){
            date.setHours(23);
            date.setMinutes(59);
            date.setSeconds(59);
        }

        List<TbUserTenementInfo> list =checkAllDao.findPayInfoList(tbUserTenementInfo);
        for(TbUserTenementInfo tbUserTeneInfo:list) {
            BigDecimal totalFee = new BigDecimal(tbUserTeneInfo.getCoveredArea()).multiply(tbUserTeneInfo.getPropertyFee());
            totalFee = totalFee.multiply(new BigDecimal(tbUserTeneInfo.getMonth())).setScale(2);
            tbUserTeneInfo.setTeneFee(totalFee);
        }
        tbUserTenementInfoPage.setList(list);
        return tbUserTenementInfoPage;
    }
    public List getListMoney(TbUserTenementInfo tbUserTenementInfo){
        List money=new ArrayList();
        Date date=tbUserTenementInfo.getEndDate();
        if(date!=null){
            date.setHours(23);
            date.setMinutes(59);
            date.setSeconds(59);
        }
        tbUserTenementInfo.setProvinceName(UserUtils.getUser().getProvinces());
        tbUserTenementInfo.setCityName(UserUtils.getUser().getCitys());

        List<TbUserTenementInfo> list=checkAllDao.findPayInfoListMoney(tbUserTenementInfo);
        BigDecimal one=new BigDecimal("0");
        BigDecimal two=new BigDecimal("0");
        BigDecimal three=new BigDecimal("0");
        BigDecimal six=new BigDecimal("0");
        BigDecimal max=new BigDecimal("0");
        for(int i=0;i<list.size();i++){
            TbUserTenementInfo tt=list.get(i);
            if("1".equals(tt.getMonth())){
                one=tt.getMoney();
            }
            if("2".equals(tt.getMonth())){
                two=tt.getMoney();
            }
            if("3".equals(tt.getMonth())){
                three=tt.getMoney();
            }
            if("6".equals(tt.getMonth())){
                six=tt.getMoney();
            }

        }
        money.add(one);
        money.add(two);
        money.add(three);
        money.add(six);
        for(int i=0;i<money.size();i++){
            if(max.compareTo((BigDecimal)money.get(i))<=0){
                max=(BigDecimal) money.get(i);
            }
        }
        money.add(max);
        return money;
    }
    //物业管理员查看他所管理的小区的物业信息
    @Transactional(readOnly = true)
    public  Page<TbUserTenementInfo> communityFee(Page<TbUserTenementInfo> tbUserTenementInfoPage, TbUserTenementInfo tbUserTenementInfo){
        tbUserTenementInfo.setPage(tbUserTenementInfoPage);
        tbUserTenementInfo.setProvinceName(UserUtils.getUser().getProvinces());
        tbUserTenementInfo.setCityName(UserUtils.getUser().getCitys());
        tbUserTenementInfo.setAreaname(UserUtils.getUser().getAreas());
        shopAddDao.userAreas(UserUtils.getUser().getId());
        if(UserUtils.getUser().getCommunityId()!=null &&!"".equals(UserUtils.getUser().getCommunityId())&&!"请选择".equals(UserUtils.getUser().getCommunityId())){
            tbUserTenementInfo.setCommunityId(Integer.parseInt(UserUtils.getUser().getCommunityId()));
        }

       /* if (tbUserTenementInfo.getBeginDate() == null){
            tbUserTenementInfo.setBeginDate(DateUtils.setDays(DateUtils.parseDate(DateUtils.getDate()), 1));
        }
        if (tbUserTenementInfo.getEndDate() == null){
            tbUserTenementInfo.setEndDate(DateUtils.addMonths(tbUserTenementInfo.getBeginDate(), 1));
        }*/
        Date date=tbUserTenementInfo.getEndDate();
        if(date!=null){
            date.setHours(23);
            date.setMinutes(59);
            date.setSeconds(59);
        }

        List<TbUserTenementInfo> list =checkAllDao.findPayInfoList(tbUserTenementInfo);
        for(TbUserTenementInfo tbUserTeneInfo:list) {
            BigDecimal totalFee = new BigDecimal(tbUserTeneInfo.getCoveredArea()).multiply(tbUserTeneInfo.getPropertyFee());
            totalFee = totalFee.multiply(new BigDecimal(tbUserTeneInfo.getMonth())).setScale(2);
            tbUserTeneInfo.setTeneFee(totalFee);
        }
        tbUserTenementInfoPage.setList(list);
        return tbUserTenementInfoPage;
    }
    public List getAllListMoney(TbUserTenementInfo tbUserTenementInfo){
        List money=new ArrayList();
        Date date=tbUserTenementInfo.getEndDate();
        if(date!=null){
            date.setHours(23);
            date.setMinutes(59);
            date.setSeconds(59);
        }

        List<TbUserTenementInfo> list=checkAllDao.findPayInfoListMoney(tbUserTenementInfo);
        BigDecimal one=new BigDecimal("0");
        BigDecimal two=new BigDecimal("0");
        BigDecimal three=new BigDecimal("0");
        BigDecimal six=new BigDecimal("0");
        BigDecimal max=new BigDecimal("0");
        for(int i=0;i<list.size();i++){
            TbUserTenementInfo tt=list.get(i);
            if("1".equals(tt.getMonth())){
                one=tt.getMoney();
            }
            if("2".equals(tt.getMonth())){
                two=tt.getMoney();
            }
            if("3".equals(tt.getMonth())){
                three=tt.getMoney();
            }
            if("6".equals(tt.getMonth())){
                six=tt.getMoney();
            }

        }
        money.add(one);
        money.add(two);
        money.add(three);
        money.add(six);
        for(int i=0;i<money.size();i++){
            if(max.compareTo((BigDecimal)money.get(i))<=0){
                max=(BigDecimal) money.get(i);
            }
        }
        money.add(max);
        return money;
    }
    public List getCommunityListMoney(TbUserTenementInfo tbUserTenementInfo){
        List money=new ArrayList();
        Date date=tbUserTenementInfo.getEndDate();
        if(date!=null){
            date.setHours(23);
            date.setMinutes(59);
            date.setSeconds(59);
        }
        tbUserTenementInfo.setProvinceName(UserUtils.getUser().getProvinces());
        tbUserTenementInfo.setCityName(UserUtils.getUser().getCitys());
        tbUserTenementInfo.setAreaname(UserUtils.getUser().getAreas());
        shopAddDao.userAreas(UserUtils.getUser().getId());
        if(UserUtils.getUser().getCommunityId()!=null &&!"".equals(UserUtils.getUser().getCommunityId())&&!"请选择".equals(UserUtils.getUser().getCommunityId())){
            tbUserTenementInfo.setCommunityId(Integer.parseInt(UserUtils.getUser().getCommunityId()));
        }

        List<TbUserTenementInfo> list=checkAllDao.findPayInfoListMoney(tbUserTenementInfo);
        BigDecimal one=new BigDecimal("0");
        BigDecimal two=new BigDecimal("0");
        BigDecimal three=new BigDecimal("0");
        BigDecimal six=new BigDecimal("0");
        BigDecimal max=new BigDecimal("0");
        for(int i=0;i<list.size();i++){
            TbUserTenementInfo tt=list.get(i);
            if("1".equals(tt.getMonth())){
                one=tt.getMoney();
            }
            if("2".equals(tt.getMonth())){
                two=tt.getMoney();
            }
            if("3".equals(tt.getMonth())){
                three=tt.getMoney();
            }
            if("6".equals(tt.getMonth())){
                six=tt.getMoney();
            }

        }
        money.add(one);
        money.add(two);
        money.add(three);
        money.add(six);
        for(int i=0;i<money.size();i++){
            if(max.compareTo((BigDecimal)money.get(i))<=0){
                max=(BigDecimal) money.get(i);
            }
        }
        money.add(max);
        return money;
    }
}
